﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Common;
using Wuqi.Webdiyer;
using Mang24k99.Model.CLMN;
using Mang24k99.Business.CLMN;
using NHibernate.Expression;
using Castle.ActiveRecord;
namespace Mang24k99.Web.CLMN.KtClmnItems
{
    public partial class ListEdit : System.Web.UI.Page
    {
        protected string RootPath = VirtualPathUtility.ToAbsolute("~/");
        string _AspNetCostumHtmlName = "AspNetCostumHtmlName";
        string _layerName = "layer";
        string _parentNoName = "parentNo";
        string _FirstNoName = "FirstNo";
        string _SecondNoName = "SecondNo";
        string _ThirdNoName = "ThirdNo";

        string _rblName = "rbl";


        public int? Rbl
        {
            get
            {
              
                return ViewStateHelper.GetInt(this.ViewState, _rblName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _rblName);
            }
        }
        string _AddName = "Add";
        string _ModifyName = "Modify";
        string _NavTextName = "NavText";
        /// <summary>
        /// 分页的名称
        /// </summary>
        string _PageIndexName = "pageIndex";
        /// <summary>
        /// 操作的名称
        /// </summary>
        string _OpName = "op";
        /// <summary>
        /// 主键的名称
        /// </summary>
        string _IDName = "ID";
        /// <summary>
        /// Gets or sets the first no.
        /// </summary>
        /// <value>The first no.</value>
        public int? FirstNo
        {
            get
            {
                return ViewStateHelper.GetInt(this.ViewState, _FirstNoName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _FirstNoName);
            }
        }
        public int? Layer
        {
            get
            {
                return ViewStateHelper.GetInt(this.ViewState, _layerName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _layerName);
            }
        }


        public int? ParentNo
        {
            get
            {
                return ViewStateHelper.GetInt(this.ViewState, _parentNoName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _parentNoName);
            }
        }
        public string AspNetCostumHtml
        {
            get
            {
                return ViewStateHelper.GetString(this.ViewState, _AspNetCostumHtmlName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _AspNetCostumHtmlName);
            }
        }

        /// <summary>
        /// Gets or sets the nav text.(保存lblNav.text的初始值)
        /// </summary>
        /// <value>The nav text.</value>
        public string NavText
        {
            get
            {
                return ViewStateHelper.GetString(ViewState, _NavTextName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _NavTextName);
            }
        }
        /// <summary>
        /// Gets or sets the index of the page.(保存修改时当前页)
        /// </summary>
        /// <value>The index of the page.</value>
        public int? PageIndex
        {
            get
            {
                return ViewStateHelper.GetInt(ViewState, _PageIndexName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _PageIndexName);
            }
        }
        /// <summary>
        /// Gets or sets the primary ID.(主键id)
        /// </summary>
        /// <value>The primary ID.</value>
        public int? PrimaryID
        {
            get
            {
                return ViewStateHelper.GetInt(ViewState, _IDName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _IDName);
            }
        }
        /// <summary>
        /// Gets or sets the op.(操作符)
        /// </summary>
        /// <value>The op.</value>
        public string Op
        {
            get
            {
                return ViewStateHelper.GetString(ViewState, _OpName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _OpName);
            }
        }


        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            
            AjaxPro.Utility.RegisterTypeForAjax(typeof(BKtClmnNewstrastar));
            if (!IsPostBack)
            {
                this.txtColumnName.Attributes.Add("onblur", "SetAlias('"+this.txtColumnName.ClientID+"','"+this.txtColumnAlias.ClientID+"')");
                InitialParameter();

            }
        }

        /// <summary>
        /// Binds the ASP net pager.(绑定分页控件)
        /// </summary>
        private void BindAspNetPager()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int RecordCount = BKtClmnItems.Count();
            aspNetPager.RecordCount = RecordCount;
            aspNetPager.CustomInfoHTML = string.Format(this.AspNetCostumHtml, aspNetPager.PageCount);
            if (!IsPostBack)
            {
                int pageIndex;
                try
                {
                    pageIndex = int.Parse(this.Request.QueryString["pageIndex"]);
                    this.AspNetPager1.CurrentPageIndex = pageIndex;

                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// Binds the reapert.(绑定列表控件)
        /// </summary>
        private void BindReapert(ICriterion[] where, int layer, int parentNo)
        {
            if (layer != 1 && parentNo == 0)//不为一级节点若父节点为0,不绑定
            {
                return;
            }
            AspNetPager aspNetPager = this.AspNetPager1;
            Repeater repeater = this.Repeater1;
            int maxRecord = aspNetPager.PageSize;
            int startRecord = aspNetPager.StartRecordIndex - 1;
            object dataSource = BKtClmnItems.FindAllByLayer(layer, parentNo);
            repeater.DataSource = dataSource;
            repeater.DataBind();
        }
        /// <summary>
        /// Handles the PageChanged event of the AspNetPager1 control.(列表改变时候)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            int layer = 1;
            int parentNo = 0;
            if (!IsPostBack)
            {
                try
                {

                    Layer = RequestHelper.GetInt(this, _layerName);
                    ParentNo = RequestHelper.GetInt(this, _parentNoName);



                    //todo:接受参数
                    SetRblList(false);



                }
                catch
                {
                    //不做处理
                }

            }

            List<ICriterion> where = new List<ICriterion>();
            int partionIndex = GetRblSelectedValue();
            int No1 = FirstNo.Value;
            int No2 = 0;
            int No3 = 0;

            try
            {

                No2 = int.Parse(this.ddlList2.SelectedValue);
                No3 = int.Parse(this.ddlList3.SelectedValue);

            }
            catch
            {

                //不做处理
            }


            switch (partionIndex)
            {
                case -1:
                    layer = 1;
                    parentNo = 0;
                    break;
                case 1:
                    layer = 2;
                    parentNo = No1;
                    break;
                case 2:
                    layer = 3;
                    parentNo = No2;
                    break;
                case 3:
                    layer = 4;
                    parentNo = No3;
                    break;
                default:
                    return;
                    break;
            }
            if (!IsPostBack)
            {
                if (Layer.HasValue)
                {
                    layer = Layer.Value;
                }
                if (ParentNo.HasValue)
                {
                    parentNo = ParentNo.Value;
                }
            }
            else
            {
                Layer = layer;
                ParentNo = parentNo;
            }

            BindAspNetPager();
            BindReapert(where.ToArray(), layer, parentNo);
        }


        /// <summary>
        /// Handles the PreRender event of the Repeater1 control.(列表程序之前)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Repeater1_PreRender(object sender, EventArgs e)
        {
            Repeater repeater = (Repeater)sender;
            IEnumerable repeaterObject = repeater.Items;
            //集体加客户端属性
            PageOp.SetRepeaterAttributes(repeaterObject, "chkIsHide", "tempID", GetPrimaryValue);
            PageOp.SetRepeaterAttributes(repeaterObject, "chkIsHide", "onclick", "return qtIndex(this)");
        }
        private string GetPrimaryValue(Control item)
        {
            Label lbl = item.FindControl("lblID") as Label;
            string value = null;
            if (lbl != null)
            {
                value = lbl.Text;
            }
            return value;
        }
        /// <summary>
        /// Handles the ItemCommand event of the Repeater1 control.(列表控件字段按钮事件)
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            Button btnSource = e.CommandSource as Button;//事件按钮
            Button btnDelete = e.Item.FindControl("btnDelete") as Button;//删除按钮
            Button btnModify = e.Item.FindControl("btnModify") as Button;
            if (btnDelete.Enabled == false)//防客户端代码失效或者恶意提交
            {
                return;
            }
            if (btnModify.Enabled == false)
            {
                return;
            }
            object id;
            try
            {
                id = int.Parse(e.CommandArgument.ToString());
            }
            catch (Exception)
            {
                //转换失败忽略
                return;
            }
            
            if (btnSource.CommandName == btnDelete.CommandName)//若为删除按钮
            {
                if (!btnDelete.Enabled)
                {
                    return;
                }
                Delete(id);
                Op = _AddName;
                MessageBox.Redirect(this, GetListUrl(this.AspNetPager1.CurrentPageIndex));
            }
            if (btnSource.CommandName == btnModify.CommandName)//为修改按钮
            {
                if (!btnModify.Enabled)
                {
                    return;
                }
                Op = _ModifyName;
                PrimaryID = TypeHelper.GetInt(id);
                PageIndex = this.AspNetPager1.CurrentPageIndex;
                BindData();
            }
        }
        protected void Delete(object id)
        {
            MKtClmnItems mKtClmnItems = BKtClmnItems.Find(id);
            if (mKtClmnItems != null)
            {
                BKtClmnItems.Delete(mKtClmnItems);
            }
        }
        protected void lnkbtnAdd_Click(object sender, EventArgs e)
        {
            GoUrl(_AddName);
        }
        private void GoUrl(string op)
        {
            string url = GetUrl(op);
            Response.Redirect(url, true);
        }
        private string GetUrl(string op)
        {
            int pageIndex = this.AspNetPager1.CurrentPageIndex;
            string url = "Edit.aspx" + "?" + "pageIndex=" + pageIndex + "&" + "op=" + op;
            return url;
        }
        protected void lnkbtnList_Click(object sender, EventArgs e)
        {
            GoServerListUrl(1);

        }
        /// <summary>
        /// Initials the parameter.(接受必要参数,无参数退出)
        /// </summary>
        private void InitialParameter()
        {
            if (string.IsNullOrEmpty(Op))
            {
                Op = _AddName;
            }
            NavText = this.lblNav.Text;
            FirstNo = RequestHelper.GetInt(this, _FirstNoName);
            Rbl = RequestHelper.GetInt(this, _rblName);
            if (FirstNo == null )
            {
                Response.End();
                return;

            }
            else
            {
                MKtClmnItems model = BKtClmnItems.Find(FirstNo);
                if (model==null)
                {
                    Response.End();
                    return;
                }
                this.lblColumnName.Text = string.Format(this.lblColumnName.Text, model.ColumnName);
            }
            if ( Rbl == null || Rbl.Value > 3 || Rbl.Value < 1)
            {
                Rbl = 1;
            }

            //PageIndex = RequestHelper.GetInt(this, _PageIndexName);
            //PrimaryID = RequestHelper.GetInt(this, _IDName);
            //if (!PageIndex.HasValue)
            //{
            //    Response.End();
            //}
            BindData();
        }
        /// <summary>
        /// Binds the data.(绑定基础数据)
        /// </summary>
        private void BindData()
        {
            this.AspNetCostumHtml = this.AspNetPager1.CustomInfoHTML;
            AspNetPager1_PageChanged(null, null);

            if (StringHelper.Compare(Op, _AddName))
            {
                this.lblNav.Text = string.Format(NavText, "新增");
                this.btnSave.Text = "新 增";
            }
            else if (StringHelper.Compare(Op, _ModifyName))
            {
                if (!PrimaryID.HasValue)
                {
                    Response.End();
                }
                this.btnSave.Text = "修 改";
                SetNavTextToModify();
                BindEdit();
            }
            else
            {
                Response.End();
            }
        }
        /// <summary>
        /// Sets the nav text to modify.
        /// </summary>
        private void SetNavTextToModify()
        {
            this.lblNav.Text = string.Format(NavText, "修改");
        }
        /// <summary>
        /// Binds the edit.(绑定编辑页面)
        /// </summary>
        private void BindEdit()
        {
            MKtClmnItems modelKtClmnItems = BKtClmnItems.Find(PrimaryID);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnName);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnAlias);
            WebHelper.SetControlValue(modelKtClmnItems, txtNewsNum);
            if (modelKtClmnItems.ChildNum>0)
            {
                this.txtSpell.Enabled = false;
                
            }
            else
            {
                this.txtSpell.Enabled = true;
            }
            this.txtSpell.Text = "";
            string values = null;
            using(new SessionScope())
            {
                this.lstStart.Items.Clear();
                foreach (MKtClmnItemstrastar item in modelKtClmnItems.MKtClmnItemstrastar)
                {
                    string text = item.MKtClmnNewstrastar.ColumnName.ToString();
                    string value = item.MKtClmnNewstrastar.ColumnID.ToString();
                    values += value + ",";
                    this.lstStart.Items.Add(new ListItem(text, value));
                }
            }
            this.txtStartValue.Text = values;
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string msg = "";
            #region 接受操作参数
            bool isAdd = false;
            if (StringHelper.Compare(Op, _AddName))
            {
                isAdd = true;
            }
            else if (StringHelper.Compare(Op, _ModifyName))
            {
                isAdd = false;
            }
            else
            {
                return;
            }
            #endregion
            if (!PageValidate.IsInt(this.txtNewsNum.Text))
            {
                msg += "好译星新闻条数不是有效的int型" + "\r\n";
            }
            if (!String.IsNullOrEmpty(msg))
            {
                MessageBox.Show(this, msg);
                return;
            }
            #region 根据操作选项创建实例
            MKtClmnItems modelKtClmnItems;
            if (isAdd)
            {
                modelKtClmnItems = new MKtClmnItems();
                modelKtClmnItems.MKtClmnItemstrastar = new List<MKtClmnItemstrastar>();
            }
            else
            {
                modelKtClmnItems = BKtClmnItems.Find(PrimaryID);
                
                if (modelKtClmnItems == null)
                {
                    return;
                }
                modelKtClmnItems.LastNum1 = modelKtClmnItems.ColumnNofirst;
                modelKtClmnItems.LastNum2 = modelKtClmnItems.ColmunNosecond;
                modelKtClmnItems.LastNum3 = modelKtClmnItems.ColumnNothird;
            }
            #endregion
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnName);
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnAlias);
            WebHelper.SetModelValue(modelKtClmnItems, txtNewsNum);
            modelKtClmnItems.TrastarNum = 0;
            modelKtClmnItems.TrastarCompos = "";
            string startValue = this.txtStartValue.Text.Trim();
            modelKtClmnItems.MKtClmnItemstrastar.Clear();
            if (startValue != "")
            {
                string[] startValues = startValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                
                foreach (string strValue in startValues)
                {
                    MKtClmnItemstrastar modelItemStart = new MKtClmnItemstrastar();

                    int? startID = TypeHelper.GetInt(strValue);
                    MKtClmnNewstrastar modelNewsStart = BKtClmnNewstrastar.Find(startID);
                    
                    if (modelNewsStart != null)
                    {
                        modelKtClmnItems.TrastarCompos += modelNewsStart.ColumnName + " ";
                        modelKtClmnItems.TrastarNum += 1;
                        if (IsHaveNewsStart(modelKtClmnItems, startID))
                        {//已经有好译星,防止添加重复记录
                            continue;
                        }
                        
                        modelItemStart.MKtClmnNewstrastar = modelNewsStart;
                        modelItemStart.MKtClmnItems = modelKtClmnItems;
                        modelItemStart.IsValidate = 1;
                        modelItemStart.InputTime = DateTime.Now;
                        modelItemStart.ModifyTime = DateTime.Now;
                        modelItemStart.DeleteTime = DateTime.Now;
                        modelKtClmnItems.MKtClmnItemstrastar.Add(modelItemStart);
                        
                        
                    }

                }
            }
            Castle.Components.Validator.RunWhen runWhen = Castle.Components.Validator.RunWhen.Update;
            if (isAdd)
            {
                modelKtClmnItems.ColumnNO = BKtClmnItems.NewID();
                modelKtClmnItems.ColumnAttrib = 1;
                modelKtClmnItems.ColumnDetailname = modelKtClmnItems.ColumnName;
                modelKtClmnItems.ColumnLayer = Rbl + 1;
                modelKtClmnItems.ColumnNofirst = FirstNo;
                modelKtClmnItems.IsReltrastar = 1;
                modelKtClmnItems.IsCreatexml = 0;
                modelKtClmnItems.IsShow = 1;
                
                modelKtClmnItems.ChildNum = 0;

                runWhen = Castle.Components.Validator.RunWhen.Insert;
            }
            msg = "";
            if (Rbl > 1)
            {
                int? SecondNo = TypeHelper.GetInt(this.ddlList2.SelectedValue);
                if (SecondNo == 0)
                {
                    msg += "请选择二级栏目名\r\n";
                }
                modelKtClmnItems.ColmunNosecond = SecondNo;
            }
            else
            {

                modelKtClmnItems.ColmunNosecond = 0;
            }
            if (Rbl > 2)
            {
                int? ThridNo = TypeHelper.GetInt(this.ddlList3.SelectedValue);
                if (ThridNo == 0)
                {
                    msg += "请选择三级栏目名\r\n";
                }
                modelKtClmnItems.ColumnNothird = ThridNo;
            }
            else
            {
                modelKtClmnItems.ColumnNothird = 0;
            }
            int? parentNo = 0;
            switch (modelKtClmnItems.ColumnLayer)
            {
                case 1:
                    parentNo = 0;
                    break;
                case 2:
                    parentNo = modelKtClmnItems.ColumnNofirst;

                    break;
                case 3:
                    parentNo = modelKtClmnItems.ColmunNosecond;

                    break;
                case 4:
                    parentNo = modelKtClmnItems.ColumnNothird;
                    break;
            }
            
            if (BKtClmnItems.ExistByName(modelKtClmnItems.ColumnLayer, parentNo, modelKtClmnItems.ColumnName, modelKtClmnItems.ColumnNO))
            {
                msg += "栏目名称重复\r\n";
            }
            if (BKtClmnItems.ExistByAlias(modelKtClmnItems.ColumnLayer, parentNo, modelKtClmnItems.ColumnAlias, modelKtClmnItems.ColumnNO))
            {
                msg += "栏目别名重复\r\n";
            }
            if (BKtClmnItems.ExistByDetailname(modelKtClmnItems.ColumnLayer, parentNo, modelKtClmnItems.ColumnDetailname, modelKtClmnItems.ColumnNO))
            {
                msg += "栏目详细名称重复\r\n";
            }
            if (!String.IsNullOrEmpty(msg))
            {
                MessageBox.Show(this, msg);
                return;
            }
           
            bool isSuccess = modelKtClmnItems.IsValid(runWhen);
            if (!isSuccess)
            {
                msg = ActiveRecordValidationBaseHelper.GetErrorMsg(modelKtClmnItems);
                MessageBox.Show(this, msg);
                return;
            }
            

            if (isAdd)
            {
                msg = "添加成功";


                BKtClmnItems.Create(modelKtClmnItems);
                MessageBox.ShowAndRedirect(this, msg, GetListUrl(0));
            }
            else
            {
                //BKtClmnItemstrastar.DeleteByClmnItem(modelKtClmnItems);
                BKtClmnItems.Update(modelKtClmnItems);
                msg = "修改成功";
                MessageBox.ShowAndRedirect(this, msg, GetListUrl(PageIndex));
            }
        }

        private void NewMethod(string[] startValues)
        {
            foreach (ListItem listItem in this.lstStart.Items)
            {
                int? targetID = TypeHelper.GetInt(listItem.Value);
                foreach (string strValue in startValues)
                {
                    int? startID = TypeHelper.GetInt(strValue);

                    if (targetID == startID)
                    {
                        //return true;
                    }
                }

            }
        }

        /// <summary>
        /// Determines whether [is have news start] [the specified model kt CLMN items].(是否有好译星栏目)
        /// </summary>
        /// <param name="modelKtClmnItems">The model kt CLMN items.</param>
        /// <param name="startID">The start ID.</param>
        /// <returns>
        /// 	<c>true</c> if [is have news start] [the specified model kt CLMN items]; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsHaveNewsStart(MKtClmnItems modelKtClmnItems, int? startID)
        {
            for (int i = 0; i < modelKtClmnItems.MKtClmnItemstrastar.Count; i++)
            {
                int? targetID = modelKtClmnItems.MKtClmnItemstrastar[i].MKtClmnNewstrastar.ColumnID;
                if (targetID == startID)
                {
                    return true;
                }
            }
            return false;
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {

            if (StringHelper.Compare(Op, _AddName))
            {
                Response.Redirect(GetListUrl(this.AspNetPager1.CurrentPageIndex));
                //MessageBox.Redirect(this, GetListUrl(this.AspNetPager1.CurrentPageIndex));
            }
            else
            {
                Response.Redirect(GetListUrl(PageIndex));
                //MessageBox.Redirect(this, GetListUrl(PageIndex));
            }
        }
        private void GoServerListUrl(int? pageIndex)
        {
            string url = GetListUrl(pageIndex);
            Response.Redirect(url, true);
        }
        private string GetListUrl(int? pageIndex)
        {
            string secondNo = ddlList2.SelectedValue;
            string thirdNo = ddlList3.SelectedValue;
            string secondNoUrl = null;
            string thirdNoUrl = null;
            if (secondNo != "0")
            {
                secondNoUrl = String.Format("&{0}={1}", _SecondNoName, secondNo);
            }
            if (thirdNo != "0")
            {
                thirdNoUrl = String.Format("&{0}={1}", _ThirdNoName, thirdNo);
            }
            string op = "?" + "" + _PageIndexName + "=" + pageIndex + "&" + _rblName + "=" + Rbl + "&" + _FirstNoName + "=" + FirstNo + secondNoUrl + thirdNoUrl;
            string url = "ListEdit.aspx" + op;
            return url;
        }

        /// <summary>
        /// Gets the RBL selected value.
        /// </summary>
        /// <returns></returns>
        private int GetRblSelectedValue()
        {

            int partionIndex = Rbl.Value;

            SetTdListVisible(partionIndex, 3);
            return partionIndex;
        }

        /// <summary>
        /// Sets the td list visible.(元素索引>partitionIndex为不显示)
        /// </summary>
        /// <param name="partitionIndex">Index of the partition.</param>
        /// <param name="maxIndex">Index of the max.</param>
        private void SetTdListVisible(int partitionIndex, int maxIndex)
        {

            Control thisControl = WebHelper.GetContentPlace(this);
            for (int i = 0; i <= partitionIndex; i++)
            {
                HtmlControl hc = thisControl.FindControl("tdDdlList" + i) as HtmlControl;
                if (hc != null)
                {
                    hc.Visible = true;
                }
            }
            for (int i = partitionIndex + 1; i <= maxIndex; i++)
            {
                HtmlControl hc = thisControl.FindControl("tdDdlList" + i) as HtmlControl;
                if (hc != null)
                {
                    hc.Visible = false;
                }

            }

        }
        private void SetRblList(bool isBindList)
        {

            int index = GetRblSelectedValue();
            string secondNo = RequestHelper.GetString(this, _SecondNoName);
            string thirdNo = RequestHelper.GetString(this, _ThirdNoName);
            if (index == -1)
            {

            }
            else if (index == 1)//选出一级
            {
                //没有一级
            }
            else if (index == 2)//选出二级
            {
                BindDdlList(true, 2, FirstNo.Value, true);

                WebHelper.SetControlValueDdl(this.ddlList2, secondNo);
            }
            else if (index == 3)//选出三级
            {
                BindDdlList(true, 2, FirstNo.Value, true);
                
                bool isHaveData = true;
                if (thirdNo == null)
                {
                    isHaveData = false;
                }
                int intSecondNo;
                if (secondNo != null && int.TryParse(secondNo,out intSecondNo))
                {
                    
                    BindDdlList(isHaveData, 3, intSecondNo, true);
                }
                else
                {
                    BindDdlList(isHaveData, 3, 0, true);
                }
                
               
                WebHelper.SetControlValueDdl(this.ddlList2, secondNo);
                WebHelper.SetControlValueDdl(this.ddlList3, thirdNo);

            }
            SetCurrentColumnStyle(index);
            if (isBindList)
            {
                AspNetPager1_PageChanged(null, null);
            }

        }

        /// <summary>
        /// Sets the current column style.(设置但前栏目样式)
        /// </summary>
        /// <param name="index">The index.</param>
        private void SetCurrentColumnStyle(int index)
        {
            this.lnkColumn1.NavigateUrl = string.Format("~/CLMN/KtClmnItems/ListEdit.aspx?firstNo={0}&Rbl={1}",FirstNo,1);
            this.lnkColumn2.NavigateUrl = string.Format("~/CLMN/KtClmnItems/ListEdit.aspx?firstNo={0}&Rbl={1}", FirstNo, 2);
            this.lnkColumn3.NavigateUrl = string.Format("~/CLMN/KtClmnItems/ListEdit.aspx?firstNo={0}&Rbl={1}", FirstNo, 3);
            this.lnkColumn4.NavigateUrl = string.Format("~/CLMN/KtClmnItems/ListOrder.aspx?firstNo={0}", FirstNo);
            Control thisControl = WebHelper.GetContentPlace(this);
            WebControl c = thisControl.FindControl("lnkColumn" + index) as WebControl;
            c.CssClass = "col_on";
        }

        /// <summary>
        /// Binds the DDL list.(index的值为1,2,3)
        /// </summary>
        /// <param name="isHaveData">if set to <c>true</c> [is have data].</param>
        /// <param name="index">The index.</param>
        /// <param name="parentNo">The parent no.</param>
        private void BindDdlList(bool isHaveData, int index, int parentNo, bool isAutoPostBack)
        {

            Control thisControl = WebHelper.GetContentPlace(this);
            DropDownList ddl = thisControl.FindControl("ddlList" + index) as DropDownList;
            if (ddl == null)
            {
                //不做处理
                return;
            }

            IList<MKtClmnItems> dataSource;
            if (isHaveData)
            {
                dataSource = BKtClmnItems.FindAllByLayerDdlList(index, parentNo);
            }
            else
            {
                dataSource = new MKtClmnItems[0];
            }
            MKtClmnItems top = new MKtClmnItems();
            top.ColumnNO = 0;
            top.ColumnName = "请选择";
            dataSource = ListHelper<MKtClmnItems>.GetList(dataSource, top);

            ddl.AutoPostBack = isAutoPostBack;
            ddl.DataSource = dataSource;
            ddl.DataTextField = "ColumnName";
            ddl.DataValueField = "ColumnNO";
            ddl.DataBind();
        }



        protected void ddlList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //根据一级栏目设置
            ParentNo = RequestHelper.GetInt(this, _parentNoName);
            if (!ParentNo.HasValue)
            {
                return;
            }

            int parentNo = ParentNo.Value;
            if (parentNo == 0)
            {
                BindDdlList(false, 2, parentNo, false);
                BindDdlList(false, 3, parentNo, false);
            }
            else if (parentNo > 0)
            {
                BindDdlList(true, 2, parentNo, true);
                BindDdlList(false, 3, parentNo, false);
            }
            else
            {
                //不做处理
            }

        }

        protected void ddlList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int parentNo = int.Parse(this.ddlList2.SelectedValue);
            if (parentNo == 0)
            {

                BindDdlList(false, 3, parentNo, false);
            }
            else if (parentNo > 0)
            {
                BindDdlList(true, 3, parentNo, true);
                if (true)
                {
                    AspNetPager1_PageChanged(null, null);
                }
            }
            else
            {
                //不做处理
            }
        }

        protected void ddlList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(null, null);
        }

        protected void IsHide_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            if (chk.Enabled==false)
            {
                return;
            }
            string primaryID = chk.Attributes["tempID"];
            MKtClmnItems model = BKtClmnItems.Find(TypeHelper.GetInt(primaryID));
            if (model == null)
            {
                return;
            }
            int isShow = 1;
            if (chk.Checked)//隐藏
            {
                isShow = 0;
            }

            model.IsShow = isShow;
            BKtClmnItems.Update(model);
        }
    }
}
